package com.s1nx.back.controller;

import com.s1nx.back.dto.UserDTO;
import com.s1nx.back.dto.convert.UserConverter;
import com.s1nx.back.entity.User;
import com.s1nx.back.service.UserService;
import com.s1nx.back.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestParam("username") String username, @RequestParam("password") String password) {

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        User login = userService.login(user);
        UserDTO userDto = UserConverter.convertToUserDTO(login);

        return Result.withCode(200).withMsg("登录成功").put("user", userDto);

    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Result register(@RequestParam("username") String username, @RequestParam("password") String password) {

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        User register = userService.register(user);

        UserDTO userDto = UserConverter.convertToUserDTO(register);
        return Result.withCode(200).withMsg("注册成功").put("user", userDto);

    }

}




























